package org.linlinjava.litemall.admin.controller.shop;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.beans.dto.ShopBindErpCodeDto;
import org.linlinjava.litemall.admin.beans.dto.ShopDto;
import org.linlinjava.litemall.admin.controller.shop.vo.AdminShopPageReqVO;
import org.linlinjava.litemall.admin.service.ShopService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.service.LitemallShopRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api("门店")
@RestController
@RequestMapping("/admin/shop")
@Validated
public class AdminShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private LitemallShopRegionService shopRegionService;

    @ApiOperation("门店列表分页")
    @GetMapping("/list")
    @LogAnno
    public ResponseUtil.Response page(@LoginAdminShopId Integer shopId, AdminShopPageReqVO pageReqVO) {
        pageReqVO.setShopId(shopId);
        return ResponseUtil.okPage(shopService.page(pageReqVO));
    }


    @ApiOperation("编辑门店")
    @PutMapping("/update")
    @LogAnno
    public Object update(@RequestBody ShopDto shop, @LoginAdminShopId Integer shopId) {
        if(null != shopId){
            shop.getLitemallShop().setId(shopId);
        }
        return shopService.update(shop);
    }


    @ApiOperation("删除门店")
    @DeleteMapping("/delete")
    @LogAnno
    public Object delete(@NotNull @LoginAdminShopId Integer id) {
        return shopService.delete(id);
    }


    @ApiOperation("添加门店")
    @PostMapping("/create")
    @LogAnno
    public Object create(@RequestBody ShopDto shop) {
        return shopService.create(shop);
    }


    @ApiOperation("门店详情")
    @GetMapping("/detail")
    @LogAnno
    public Object detail(@NotNull @LoginAdminShopId Integer id) {
        return shopService.detail(id);
    }


    @ApiOperation("所有门店")
    @GetMapping("/allForPerm")
    @LogAnno
    public Object all(@LoginAdminShopId Integer id) {
        List<LitemallShop> shops = shopService.all(id);
        return ResponseUtil.okList(shops);
    }


/*    @GetMapping("/merchandiseNumber")
    @LogAnno
    public Object all(@LoginAdminShopId Integer id, String merchandiseSn) {
        return ResponseUtil.ok(shopMerchandiseService.queryBySn(merchandiseSn, id));
    }*/


    @ApiOperation("查询门店商品信息")
    @GetMapping("/shopOverview")
    @LogAnno
    public Object queryShopGoodsInfo(@LoginAdminShopId @NotNull Integer id){
        return ResponseUtil.ok(shopService.querShopGoodsInfo(id));
    }


    @ApiOperation("查询门店位置信息")
    @GetMapping("/region")
    @LogAnno
    public Object queryShopRegions(@LoginAdminShopId @NotNull Integer id){
        return ResponseUtil.okList(shopRegionService.queryByShopId(id));
    }


    @ApiOperation("查询门店运维信息")
    @GetMapping("/ops")
    @LogAnno
    public Object queryShopOps(@LoginAdminShopId Integer id){
        return shopService.queryShopOps(id);
    }


    @ApiOperation("绑定ERP店铺编码")
    @PostMapping("/bindErpCode")
    @LogAnno
    public Object bindErpCode(@Valid @RequestBody ShopBindErpCodeDto shopBindErpCodeDto) {
        return shopService.bindErpCode(shopBindErpCodeDto);
    }


    @ApiOperation("复制店铺")
    @GetMapping("/copy")
    @LogAnno
    public Object copy(@NotNull Integer id) {
    	return shopService.copy(id);
    }


    @ApiOperation("查询所有门店位置")
    @GetMapping("shopLocations")
    @LogAnno
    public Object shopLocations(){
        return shopService.locations();
    }
}
